#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>

#include "scheduler.h"
#include "global.h"
#include "xlate.h"

void ShutDown( void ) // Shut down threads 
{
	// Tell thread to die
	ReleaseMutex( v4RunMutex );
	ReleaseMutex( v6RunMutex );
}

void KbdFunc( void ) // Dispatch and count threads.
{
	int KeyInfo;

	// Begin threads
	_beginthread( xlate4to6, 0, NULL );
	_beginthread( xlate6to4, 0, NULL );

	// Loop and wait
	do {
		KeyInfo = _getch();
	} while( tolower( KeyInfo ) != 'q' );

	ShutDown();
}

void Scheduler( void )
{
	// Create the mutexes
	v4RunMutex = CreateMutex( NULL, TRUE, NULL );      // Set
	v6RunMutex = CreateMutex( NULL, TRUE, NULL );      // Set

	// Start waiting for keyboard input to dispatch threads or exit.
	KbdFunc();

	// All threads done. Clean up handles.
	CloseHandle( v4RunMutex );
	CloseHandle( v6RunMutex );
}